﻿using System;
using System.Security.Cryptography;
using System.Web;
using System.Web.Security;
using GC_RF.Domain.Abstractions.Interfaces.Helpers;

namespace GC_RF.Domain.Helpers
{
	public class CryptographicAdapter : ICryptographicAdapter
	{
		#region ICryptographicAdapter Members

		public string CreateSalt()
		{
			var rng = new RNGCryptoServiceProvider();
			var buff = new byte[32];
			rng.GetBytes(buff);

			return Convert.ToBase64String(buff);
		}

		public string CreatePasswordHash(string password, string salt)
		{
			return FormsAuthentication.HashPasswordForStoringInConfigFile(string.Concat(password, salt), "SHA1");
		}

		public string GenerateEncodedUniqueToken()
		{
			var randomSequence = new byte[12];
			using (var cryptoProvider = new RNGCryptoServiceProvider())
			{
				cryptoProvider.GetBytes(randomSequence);
			}
			return HttpServerUtility.UrlTokenEncode(randomSequence);
		}

		#endregion
	}
}
